import 'dart:async';

import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:part_time_job_pro/common/base/base.dart';
import 'package:part_time_job_pro/common/index.dart';
import 'package:part_time_job_pro/pages/forget/state.dart';

class ForgetLogic extends BaseController {
  final ForgetState state = ForgetState();

  //定时器
  late Timer _timer;
  late TextEditingController mobileEditController;
  late TextEditingController verifyCodeEditController;
  late TextEditingController passwordEditController;
  late TextEditingController repeatPasswordEditController;

  ///监听是否正在输入数据
  late FocusNode mobileFocusNode;
  late FocusNode verifyCodeFocusNode;
  late FocusNode passwordFocusNode;
  late FocusNode repeatPasswordFocusNode;

  //初始化
  @override
  void onInit() {
    initFocusNode();
    super.onInit();
  }

  //注销函数
  @override
  void onClose() {
    mobileEditController.dispose();
    repeatPasswordEditController.dispose();
    passwordEditController.dispose();
    verifyCodeEditController.dispose();
    super.onClose();
  }

  ///监听是否在输入密码或验证码
  initFocusNode() {
    mobileEditController = TextEditingController();
    verifyCodeEditController = TextEditingController();
    passwordEditController = TextEditingController();
    repeatPasswordEditController = TextEditingController();

    mobileFocusNode = FocusNode();
    passwordFocusNode = FocusNode();
    verifyCodeFocusNode = FocusNode();
    repeatPasswordFocusNode = FocusNode();

    ///输入监听
    mobileFocusNode.addListener(() {
      if (mobileFocusNode.hasFocus) {
        state.isInputPassword = false;
      } else {
        state.isInputPassword = true;
      }
      update();
    });

    passwordFocusNode.addListener(() {
      if (passwordFocusNode.hasFocus) {
        state.isInputPassword = true;
      } else {
        state.isInputPassword = false;
      }
      update();
    });

    repeatPasswordFocusNode.addListener(() {
      if (repeatPasswordFocusNode.hasFocus) {
        state.isInputPassword = true;
      } else {
        state.isInputPassword = false;
      }
      update();
    });

    verifyCodeFocusNode.addListener(() {
      if (verifyCodeFocusNode.hasFocus) {
        state.isInputPassword = true;
      } else {
        state.isInputPassword = false;
      }
      update();
    });
  }

  ///监听手机号输入
  updateMobile(String text) {
    state.mobileText = text;
    update();
  }

  //清空手机号输入
  clearMobile() {
    state.mobileText = '';
    mobileEditController.clear();
    mobileFocusNode.unfocus();
    update();
  }

  ///监听验证码输入
  updateVerifyCode(String text) {
    state.verifyText = text;
    update();
  }

  //清空验证码输入
  clearVerifyCode() {
    state.verifyText = '';
    verifyCodeEditController.clear();
    verifyCodeFocusNode.unfocus();
    update();
  }

  ///监听密码输入
  updatePassword(String text) {
    state.passwordText = text;
    update();
  }

  //清空密码输入
  clearPassword() {
    state.passwordText = '';
    passwordEditController.clear();
    passwordFocusNode.unfocus();
    update();
  }

  ///监听确认密码输入
  updateRepeatPassword(String text) {
    state.repeatPasswordText = text;
    update();
  }

  //清空确认密码输入
  clearRepeatPassword() {
    state.repeatPasswordText = '';
    repeatPasswordEditController.clear();
    repeatPasswordFocusNode.unfocus();
    update();
  }

  //修改密码隐藏
  updateIsObscure() {
    state.isObscure = !state.isObscure;
    update();
  }

  //修改密码隐藏
  updateRepIsObscure() {
    state.isRepObscure = !state.isRepObscure;
    update();
  }

  startTimer(int seconds) {
    //设置 1 秒回调一次
    const period = Duration(seconds: 1);
    _timer = Timer.periodic(period, (timer) {
      //秒数减一，因为一秒回调一次
      seconds--;
      state.codeSeconds = seconds;
      if (seconds == 0) {
        //倒计时秒数为0，取消定时器
        cancelTimer();
      }
      update();
    });
  }

  //清除定时器
  cancelTimer() {
    _timer.cancel();
  }

  ///发送短信验证码
  getSmsCode() {
    //先清除输入框
    clearVerifyCode();
    if (state.mobileText.isEmpty) {
      EasyLoading.showToast("请输入手机号码");
      return;
    }
    //先进行验证 然后发送短信验证码
    SmsApi().sendSmsCode((data) {
      int resultCode = data[ResponesCode.codeTag];
      var resultMsg = data[ResponesCode.msgTag];
      if (resultCode == ResponesCode.successCode) {
        var resultData = data[ResponesCode.dataTag];
        EasyLoading.showToast("发送成功!");
        startTimer(resultData);
      } else {
        EasyLoading.showToast("发送失败: $resultMsg");
      }
    }, state.mobileText, type: "1");
  }

  //重置密码
  submitResetPasword() {
    ///验证参数
    if (state.mobileText.isEmpty) {
      EasyLoading.showToast("请输入手机号码");
      return;
    }

    if (state.verifyText.isEmpty) {
      EasyLoading.showToast("请输入短信验证码");
      return;
    }

    if (state.passwordText.isEmpty) {
      EasyLoading.showToast("请输入新的密码");
      return;
    }

    if (state.repeatPasswordText.isEmpty) {
      EasyLoading.showToast("请输入确认密码");
      return;
    }

    //验证密码是否一致
    if (state.passwordText != state.repeatPasswordText) {
      EasyLoading.showToast("您两次输入的密码不一致");
      return;
    }

    //处理参数
    Map<String, dynamic> params = {
      "mobile": state.mobileText,
      "smscode": state.verifyText,
      "password": CommonUtil.generateMd5(state.passwordText),
    };
    //发送请求
    UserApi().resetAccPassword((data) {
      LogUtil.e(data, tag: "返回数据");
      if (data is Map) {
        int resultCode = data[ResponesCode.codeTag];
        String resultMsg = data[ResponesCode.msgTag];
        if (resultCode == ResponesCode.successCode) {
          EasyLoading.showToast("重置密码成功!");
          Future.delayed(const Duration(seconds: 1)).whenComplete(() {
            Get.offAndToNamed(AppRoutes.userLogin);
          });
        } else {
          EasyLoading.showToast(resultMsg);
        }
      }
    }, params);
  }
}
